/**
 * @namespace
 */
aws.modules.ui = {
	listSubscribers: function() {
		return {
			"init": this.init,
			"keypress": this.keypress,
			"rootWaypointSet": this.show,
			"goneToNextWaypoint": this.goneToNextWaypoint,
			"goneToPreviousWaypoint": this.goneToPreviousWaypoint,
			"waypointValueChanged": this.waypointValueChanged,
			"rootWaypointUnset": [this.rootWaypointUnset, this.hide, this.reset],
			"reset": [this.hide, this.reset]
		}
	},
	/**
	 * Prepends UI's panel and subscribes to various events
	 */
	init: function() {
		$('<div id="aws"><div class="container"><ul class="path" /></div></div>').prependTo('body');
	},
	/**
	 * Simply shows UI panel
	 */
	show: function() {
		$('#aws').show();
	},
	/**
	 * Hides UI panel
	 */
	hide: function() {
		$('#aws').hide();
	},
	/**
	 * Removes info about waypoints
	 */
	reset: function() {
		$('#aws ul.path').empty();
	},
	/**
	 * Prevents browser to go to previous page when it should not
	 * @param {String} code Code of key pressed
	 * @param {Object} e Event
	 */
	keypress: function(code, e) {
		if (code == 8 && (this.waypoint || this.modules.ui.preventNextBackspace) ) {
			e.preventDefault();
			delete this.modules.ui.preventNextBackspace;
		}
	},
	/**
	 * Appends info about new waypoint to UI panel
	 */
	goneToNextWaypoint: function() {
		var html = '<li>';
		if (typeof this.waypoint.value != 'undefined') {
			 html = '<li class="value">';
		}
		html += '<span class="waypoint-name">' + this.waypoint.name + '</span>';
		if (this.waypoint.onEnter) {
			html += '<span class="waypoint-onenter">↳ ' + this.waypoint.onEnter.description + '</span>';
		}
		if (this.waypoint.next) {
			html += '<span class="waypoint-next">→ '
			if (typeof this.waypoint.next.value != 'undefined') {
				html += 'enter value';
			} else {
				var allnext = [];
				$.each(this.waypoint.next, function(k, v) {
					allnext.push(v.name + '(' + k + ')');
				});
				html += allnext.join(', ');
			}
			html += '</span>';
		}
		html += '<span class="waypoint-description">' + this.waypoint.description + '</span>';
		html += '</li>';
		$('#aws ul.path').append(html);
	},
	/**
	 * Removes info about last waypoint from UI panel
	 */
	goneToPreviousWaypoint: function() {
		$('#aws ul.path li:last').remove();
	},
	/**
	 * Displays new value of waypoint in UI panel
	 */
	waypointValueChanged: function() {
		$('#aws ul.path li:last span.waypoint-name').text(this.waypoint.value);
	},
	/**
	 * Sets temporary flag to inform ui.keypress to ignore next backspace, because it will be executed after resetting
	 */
	rootWaypointUnset: function() {
		this.modules.ui.preventNextBackspace = true;
	}
};
